package com.example.developerlab;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.Utils;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class MainActivity extends AppCompatActivity {
	private static final String TAG = MainActivity.class.getSimpleName();

	private static final int REQUEST_CAMERA_PERMISSION_DONE = 1001;

	private Handler permissionHandler = new Handler(Looper.myLooper()) {
		@Override
		public void handleMessage(@NonNull Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case REQUEST_CAMERA_PERMISSION_DONE: // 当请求完一个权限再触发下一个权限的申请
					MainActivityPermissionsDispatcher.getStoragePermissionWithPermissionCheck(MainActivity.this);
					break;
				default:
					Log.w(TAG, "Undefine message id:" + msg.what);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Utils.init(getApplication());
		initView();
	}

	private void initView() {
		Button btnCalendar = findViewById(R.id.btn_calendar);
		btnCalendar.setOnClickListener(v -> {
			Intent intent = new Intent(MainActivity.this, CalendarActivity.class);
			startActivity(intent);
		});

		Button btnTextViewSpan = findViewById(R.id.btn_textViewSpanTest);
		btnTextViewSpan.setOnClickListener(v -> {
			Intent intent = new Intent(MainActivity.this, TextViewSpanActivity.class);
			startActivity(intent);
		});

		TextView copyView = findViewById(R.id.copy_view);
		copyView.setText("lalala");


		Button btnDialogTest = findViewById(R.id.btn_dialog);
		btnDialogTest.setOnClickListener(v -> {
			new CustomDialog().showDialogWithConfig(this);
		});


		Button btnDial = findViewById(R.id.btn_dial_activity);
		btnDial.setOnClickListener(v -> {
			dialPhoneNumber("10086");
		});

		Button btnCall = findViewById(R.id.btn_call_phone);
		btnCall.setOnClickListener(v -> {
			callPhoneNumberWithPermissionCheck("10086");
		});

		Button btnActivityCompat = findViewById(R.id.btn_activity_compat);
		btnActivityCompat.setOnClickListener(v -> {
			Intent intent = new Intent(MainActivity.this, ActivityCompatPermissionTestActivity.class);
			startActivity(intent);
		});

		Button btnActivity = findViewById(R.id.btn_activity);
		btnActivity.setOnClickListener(v -> {
			Intent intent = new Intent(MainActivity.this, ActivityPermissionTestActivity.class);
			startActivity(intent);
		});

		Button btnPermissionDispatcher = findViewById(R.id.btn_permission_dispatcher);
		btnPermissionDispatcher.setOnClickListener(v -> {
			MainActivityPermissionsDispatcher.getCameraPermissionWithPermissionCheck(this);
		});

		Button btnFragment = findViewById(R.id.btn_fragment);
		btnFragment.setOnClickListener(v -> {
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
			fragmentTransaction.replace(R.id.fragment_permission, new FragmentPermission());
			fragmentTransaction.commit();
		});

		Button btnCustomDialog = findViewById(R.id.btn_custom_dialog);
		btnCustomDialog.setOnClickListener(v -> CustomizedDialog.initCustomDialogNew(MainActivity.this));

		Button btnEasyPermission = findViewById(R.id.btn_permission_easypermission);
		btnEasyPermission.setOnClickListener(v -> {
			Intent intent = new Intent(MainActivity.this, EasyPermissionTestActivity.class);
			startActivity(intent);
		});

		Button btnWebView = findViewById(R.id.btn_web_view);
		btnWebView.setOnClickListener(v -> {
			Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
			startActivity(intent);
		});

		Button btnHeadset = findViewById(R.id.btn_headset);
		btnHeadset.setOnClickListener(v -> {
			Intent intent = new Intent(MainActivity.this, AudioManagerTest.class);
			startActivity(intent);
		});
	}

	public void dialPhoneNumber(String phoneNumber) {
		Intent intent = new Intent(Intent.ACTION_DIAL);
		intent.setData(Uri.parse("tel:" + phoneNumber));
		if (intent.resolveActivity(getPackageManager()) != null) {
			startActivity(intent);
		}
	}

	@SuppressLint("WrongConstant")
	public void callPhoneNumberWithPermissionCheck(String phoneNumber) {
		if (PermissionUtils.isGranted(Manifest.permission.CALL_PHONE)) {
			callPhoneNumber(phoneNumber);
		} else {
			PermissionUtils.permission(Manifest.permission.CALL_PHONE).callback(new PermissionUtils.SimpleCallback() {
				@Override
				public void onGranted() {
					callPhoneNumber(phoneNumber);
				}

				@Override
				public void onDenied() {
					Toast.makeText(MainActivity.this, "电话权限被拒绝", Toast.LENGTH_SHORT).show();
					PermissionUtils.openAppSettings();
				}
			}).request();
		}
	}

	public void callPhoneNumber(String phoneNumber) {
		Intent intent = new Intent(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + phoneNumber));
		if (intent.resolveActivity(getPackageManager()) != null) {
			startActivity(intent);
		}
	}

	@NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.READ_EXTERNAL_STORAGE})
	public void getStoragePermission() {
		Log.i(TAG, "getStoragePermission");
	}

	@NeedsPermission(Manifest.permission.CAMERA)
	public void getCameraPermission() {
		Log.i(TAG, "getCameraPermission");
		notifyRequestCameraPermission();
	}

	@OnPermissionDenied(Manifest.permission.CAMERA)
	public void cmeraPermissionDenied() {
		Log.i(TAG, "cmeraPermissionDenied");
		notifyRequestCameraPermission();
	}

	@OnNeverAskAgain(Manifest.permission.CAMERA)
	public void cmeraNeverAskAgain() {
		Log.i(TAG, "cmeraNeverAskAgain");
		notifyRequestCameraPermission();
	}

	private void notifyRequestCameraPermission() {
		Message msg = Message.obtain(permissionHandler);
		msg.what = REQUEST_CAMERA_PERMISSION_DONE;
		msg.sendToTarget();
	}
}